<template>
  <div class="app-container">
    <div class="Search">
      <!--工具条-->
      <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true" :model="query">
          <el-form-item>
            <el-input v-model="query.params.id" placeholder="主键" />
          </el-form-item>
          <el-form-item>
            <el-input v-model="query.params.goodsId" placeholder="关联商品" />
          </el-form-item>
          <el-form-item>
            <el-input v-model="query.params.imgUrl" placeholder="图片所在地址" />
          </el-form-item>
          <el-form-item>
            <el-select v-model="query.params.imgType" placeholder="图片类型">
              <el-option v-for="item in query.options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input v-model="query.params.folder" placeholder="目录" />
          </el-form-item>
          <el-form-item>
            <el-select v-model="query.params.sort" placeholder="排序">
              <el-option v-for="item in query.options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input v-model="query.params.organId" placeholder="所属组织" />
          </el-form-item>
          <el-form-item>
            <el-input v-model="query.params.organPath" placeholder="组织路径" />
          </el-form-item>
          <el-form-item>
            <el-input v-model="query.params.createBy" placeholder="生成者" />
          </el-form-item>
          <el-form-item>
            <el-date-picker v-model="query.params.createDate" type="date" placeholder="生成时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-date-picker v-model="query.params.createDate" type="date" placeholder="生成时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-input v-model="query.params.updateBy" placeholder="更新者" />
          </el-form-item>
          <el-form-item>
            <el-date-picker v-model="query.params.updateDate" type="date" placeholder="更新时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-date-picker v-model="query.params.updateDate" type="date" placeholder="更新时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-input v-model="query.params.remark" placeholder="备注" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="fetchData">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="success" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </div>
    <div class="block">
      <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
        <el-table-column align="center" label="序号" width="95">
          <template slot-scope="scope">
            {{ scope.$index }}
          </template>
        </el-table-column>
        <el-table-column label="主键" width="110">
          <template slot-scope="scope">
            {{ scope.row.id }}
          </template>
        </el-table-column>
        <el-table-column label="关联商品" width="110">
          <template slot-scope="scope">
            {{ scope.row.goodsId }}
          </template>
        </el-table-column>
        <el-table-column label="图片所在地址" width="110">
          <template slot-scope="scope">
            {{ scope.row.imgUrl }}
          </template>
        </el-table-column>
        <el-table-column label="图片类型" width="110">
          <template slot-scope="scope">
            {{ scope.row.imgType }}
          </template>
        </el-table-column>
        <el-table-column label="目录" width="110">
          <template slot-scope="scope">
            {{ scope.row.folder }}
          </template>
        </el-table-column>
        <el-table-column label="排序" width="110">
          <template slot-scope="scope">
            {{ scope.row.sort }}
          </template>
        </el-table-column>
        <el-table-column label="所属机构" width="110" prop="organId" :formatter="translateOrgan" />
        <el-table-column label="生成时间" width="110">
          <template slot-scope="scope">
            {{ scope.row.createDate }}
          </template>
        </el-table-column>
        <el-table-column label="更新者" width="110">
          <template slot-scope="scope">
            {{ scope.row.updateBy }}
          </template>
        </el-table-column>
        <el-table-column label="更新时间" width="110">
          <template slot-scope="scope">
            {{ scope.row.updateDate }}
          </template>
        </el-table-column>
        <el-table-column label="删除标识" width="110">
          <template slot-scope="scope">
            {{ scope.row.isDeleted }}
          </template>
        </el-table-column>
        <el-table-column label="备注" width="110">
          <template slot-scope="scope">
            {{ scope.row.remark }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="created_at" label="操作" width="200">
          <template slot-scope="scope">
            <span class="el-tag el-tag--info el-tag--mini" style="cursor: pointer;" @click="handleEdit(scope.$index, scope.row)">
              {{ scope.row.isSet?'保存':"修改" }}
            </span>
            <span v-if="!scope.row.isSet" class="el-tag el-tag--danger el-tag--mini" style="cursor: pointer;" @click="handleDelete(scope.$index, scope.row)">
              删除
            </span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination :current-page="currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>
    <div class="block">
      <!--弹框-->
      <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item v-if="addForVisible" label="角色Id">
            <el-input v-model="form.id" auto-complete="off" />
          </el-form-item>
          <el-form-item label="主键">
            <el-input v-model="form.id" placeholder="主键" auto-complete="off" />
          </el-form-item>
          <el-form-item label="关联商品">
            <el-input v-model="form.goodsId" placeholder="关联商品" auto-complete="off" />
          </el-form-item>
          <el-form-item label="图片所在地址">
            <el-input v-model="form.imgUrl" placeholder="图片所在地址" auto-complete="off" />
          </el-form-item>
          <el-form-item label="图片类型">
            <el-select v-model="form.imgType" placeholder="图片类型">
              <el-option v-for="item in query.options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目录">
            <el-input v-model="form.folder" placeholder="目录" auto-complete="off" />
          </el-form-item>
          <el-form-item label="排序">
            <el-select v-model="form.sort" placeholder="排序">
              <el-option v-for="item in query.options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="所属组织">
            <el-input v-model="form.organId" placeholder="所属组织" auto-complete="off" />
          </el-form-item>
          <el-form-item label="组织路径">
            <el-input v-model="form.organPath" placeholder="组织路径" auto-complete="off" />
          </el-form-item>
          <el-form-item label="生成者">
            <el-input v-model="form.createBy" placeholder="生成者" auto-complete="off" />
          </el-form-item>
          <el-form-item label="生成时间">
            <el-date-picker v-model="form.createDate" type="date" placeholder="生成时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="更新者">
            <el-input v-model="form.updateBy" placeholder="更新者" auto-complete="off" />
          </el-form-item>
          <el-form-item label="更新时间">
            <el-date-picker v-model="form.updateDate" type="date" placeholder="更新时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" placeholder="备注" auto-complete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleEditSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getList, doDelete, doEdit } from '@/api/GoodsImg/'
import { queryAssigneeOrgan, querySubordinateOrgan } from '@/api/Organ'
import { getOrganId, getTenantId, getOrganPath, getCurrentLoginUserId, handleBuildTreeDataStruct, getSelectedNodes, getSelectedNodesObject } from '@/api/utils'
import { getTenantList, getOrganList, getDictionary, translateOrganId2OrganName } from '@/utils/auth'
import localStorageUtil from '@/utils/localStorageUtil'


export default {
  data() {
    return {
      list: null,
      listLoading: true,
      total: 5,
      currentPage: 1,
      pageSize: 10,
      dialogFormVisible: false,
      addForVisible: false,
      form: {
        id: '',
        goodsId: '',
        imgUrl: '',
        imgType: '',
        folder: '',
        sort: '',
        organId: '',
        organPath: '',
        createBy: '',
        createDate: '',
        updateBy: '',
        updateDate: '',
        isDeleted: '',
        remark: ''
      },
      // 新增or编辑弹框标题(根据点击的新增or编辑按钮显示不同的标题)
      titleMap: {
        add: '新增',
        edit: '编辑'
      },
      // 新增和编辑弹框标题
      dialogStatus: '',
      query: {
        params: {
          id: '',
          goodsId: '',
          imgUrl: '',
          imgType: '',
          folder: '',
          sort: '',
          organId: '',
          organPath: '',
          createBy: '',
          createDate: '',
          updateBy: '',
          updateDate: '',
          isDeleted: '',
          remark: ''
        },
        timeValue: '',
        options: [{
          value: '1',
          label: '黄金糕'
        }, {
          value: '2',
          label: '双皮奶'
        }],
        value: ''
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    translateOrgan(row) {
      return translateOrganId2OrganName(row);
    },
    fetchData() {
      this.listLoading = true
      getList(this.query.params, this.currentPage, this.pageSize).then(response => {
        const data = response.data
        this.total = data.totalCount
        this.pageSize = data.pageSize
        this.currentPage = data.currentPage
        this.list = data.entities
        this.listLoading = false
      })
    },
    handleSizeChange(val) {
      this.currentPage = 1
      this.pageSize = val
    },
    handleCurrentChange(val) {
      this.currentPage = val
    },
    // 新增
    handleAdd() {
      // 显示弹框
      this.dialogFormVisible = true
      // 新增弹框标题
      this.dialogStatus = 'add'

      this.form.id = ''
      this.form.goodsId = ''
      this.form.imgUrl = ''
      this.form.imgType = ''
      this.form.folder = ''
      this.form.sort = ''
      this.form.organId = ''
      this.form.organPath = ''
      this.form.createBy = ''
      this.form.createDate = ''
      this.form.updateBy = ''
      this.form.updateDate = ''
      this.form.isDeleted = ''
      this.form.remark = ''
      this.addForVisible = false
    },
    // 编辑
    handleEdit(index, row) {
      console.log(index)
      console.log(row)
      this.form.id = row.id
      this.form.goodsId = row.goodsId
      this.form.imgUrl = row.imgUrl
      this.form.imgType = row.imgType
      this.form.folder = row.folder
      this.form.sort = row.sort
      this.form.organId = row.organId
      this.form.organPath = row.organPath
      this.form.createBy = row.createBy
      this.form.createDate = row.createDate
      this.form.updateBy = row.updateBy
      this.form.updateDate = row.updateDate
      this.form.isDeleted = row.isDeleted
      this.form.remark = row.remark
      this.addForVisible = true
      // 显示弹框
      this.dialogFormVisible = true
      // 编辑弹框标题
      this.dialogStatus = 'edit'
    },
    handleDelete(index, row) {
      console.log(index)
      console.log(row)

      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        doDelete(row.id).then(response => {
          const gateway = response.data
          let data = gateway.data
          if (data.successful) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.fetchData();
          } else {
            this.$message({
              type: 'info',
              message: data.description || getSystemErrorCode(data.status)
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleEditSave() {
      debugger

      doEdit(this.form).then(response => {
        const gateway = response.data
        let data = gateway.data
        if (data.successful) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.fetchData();
        } else {
          this.$message({
            type: 'info',
            message: data.description || getSystemErrorCode(data.status)
          })
        }
        this.listLoading = false
        this.dialogFormVisible = false
      })
    },
    handleReset() {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}

</script>
